package at.fhooe.im440.pandora.time;

import at.fhooe.im440.pandora.core.Core;
import at.fhooe.im440.pandora.events.GameEvent;
import at.fhooe.im440.pandora.events.GameEventListener;

public class Time implements GameEventListener {
	private long gameStartupTime;
	private long pauseStartupTime;
	
	private long currentPauseTime;
	private long prevPauseTime;
	
	private boolean isPaused;
	
	private Core core;
	
	public Time(Core c)
	{
		this.core = c;
		setUp();
		startGameTime();
		isPaused = false;
	}
	
	public float getTotalGameTime() {
		return System.currentTimeMillis()-gameStartupTime;
	}
	
	public float getTotalPauseTime(){
		if(isPaused) {
			currentPauseTime = System.currentTimeMillis()-pauseStartupTime;
			return currentPauseTime;
		} else { 
			prevPauseTime = currentPauseTime;
			return prevPauseTime;
		}
	}
	
	public void startGameTime(){
		gameStartupTime = System.currentTimeMillis();
	}
	
	public void startPauseTime(){
		pauseStartupTime = System.currentTimeMillis() - prevPauseTime;
	}
	
	private void pauseGame() 
	{
		isPaused = true;
		startPauseTime();
	}
	
	private void awakeGame()
	{
		isPaused = false;
	}

	@Override
	public String getType() {
		return "GameEvent";
	}

	@Override
	public void setUp() {
		core.eventManager.addListener(this, "GameEvent");
	}

	@Override
	public void tearDown() {
		core.eventManager.removeListener(this);
	}
	
	@Override
	public void OnGameEvent(GameEvent e) {
		switch (e.getGameEvent())
		{
		case PAUSESTART:
			pauseGame();
			break;
		case PAUSEEND:
			awakeGame();
			break;
		default:
			break;
		}
		
	}

}
